package com.bnyy.medicalHousekeeper.moudle.message.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.common.ResponseData;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.RefreshFragment;
import com.bnyy.medicalHousekeeper.moudle.message.adapter.FocusOrFansAdapter;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactUser;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import rx.Observable;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/12/26
 **/
public class FocusOrFansFragment extends RefreshFragment {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int type;
    private int userId;
    private FocusOrFansAdapter adapter;

    public static FocusOrFansFragment getInstance(int userId, int type) {
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("userId", userId);
        FocusOrFansFragment fragment = new FocusOrFansFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                HashMap<String, Object> params = new HashMap<>();
                params.put("id", userId);
                Observable<ResponseData<ArrayList<ContactUser>>> observable;
                if (type == 0) {
                    observable = requestManager.mMessageRetrofitService.getFocusList(RequestManager.getJsonRequestBody(params));
                } else {
                    observable = requestManager.mMessageRetrofitService.getFansList(RequestManager.getJsonRequestBody(params));
                }
                requestManager.request(observable,
                        new BaseObserverImpl<ArrayList<ContactUser>>(mRefreshLayout) {
                            @Override
                            public void onSuccess(ArrayList<ContactUser> contactUsers) {
                                super.onSuccess(contactUsers);
                                if (adapter == null) {
                                    adapter = new FocusOrFansAdapter(mContext);
                                    recyclerView.setAdapter(adapter);
                                }
                                adapter.refresh(contactUsers);

                                if (contactUsers.size() == 0) {
                                    llNoData.setVisibility(View.VISIBLE);
                                }
                            }
                        });
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return null;
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        type = getArguments().getInt("type", 0);
        userId = getArguments().getInt("userId", 0);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (adapter == null) {
            mRefreshLayout.autoRefresh();
        }
    }
}

















































